<?php
namespace Application\Model;

use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Select;
use Zend\Db\ResultSet\ResultSet;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\Adapter\AdapterInterface;

class OrderProductTable
{

    protected $tableGateway;

    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
    }

    public function getOrderProductbyId($id)
    {
        $id = (int) $id;
        $rowset = $this->tableGateway->select(array(
            'id' => $id
        ));
        $row = $rowset->current();
        if (! $row) {
            throw new \Exception("Could not find row option $id");
        }
        return $row;
    }
    public function getArrayProductByOrderId($orderId)
    {

        $arrayProduct=array();
        $orderId = (int) $orderId;
        $rowset = $this->tableGateway->select(array(
            'order_id' => $orderId
        ));

       foreach ($rowset as $key => $row) {

       	array_push($arrayProduct, $row->product_id);
       }

        if (empty($arrayProduct)) {
           return null;
        }

        return $arrayProduct;
    }
    public function getOrderProductBySecurityCode($securityCode,$productId)
    {

        $arrayProduct=array();
        $orderId = (int) $orderId;
        $rowset = $this->tableGateway->select(array(
            'security_code' => $securityCode,
            'product_id' => $productId
        ));

       $row = $rowset->current();
        if (! $row) {
        return null;
        }
        return $row;
    }



    public function fetchAll($paginated = false)
    {
        if ($paginated) {
            // create a new Select object for the table album
            $select = new Select('order_product');

            // create a new result set based on the Album entity
            $resultSetPrototype = new ResultSet();
            $resultSetPrototype->setArrayObjectPrototype(new OrderProduct());
            // create a new pagination adapter object
            $paginatorAdapter = new DbSelect(
                // our configured select object
                $select,
                // the adapter to run it against
                $this->tableGateway->getAdapter(),
                // the result set to hydrate
                $resultSetPrototype);
            $paginator = new Paginator($paginatorAdapter);
            return $paginator;
        }
        $resultSet = $this->tableGateway->select();
        return $resultSet;
    }




    public function saveOrderProduct(OrderProduct $orderproduct)
    {
        $data = array(
            'product_id' => $orderproduct->product_id,
            'total_item' => $orderproduct->total_item,
            'price' => $orderproduct->price,
            'order_id' => $orderproduct->order_id,
            'security_code' => $orderproduct->security_code,
            'total_download' => $orderproduct->total_download

        );

        $id = (int) $orderproduct->id;

        if ($id == 0) {
            $this->tableGateway->insert($data);
        } else {
            if ($this->getOrderProductbyId($id)) {
                $this->tableGateway->update($data, array(
                    'id' => $id
                ));
            } else {
                throw new \Exception('Form id does not exist');
            }
        }
    }

    public function deleteOrderProduct($id)
    {
        $this->tableGateway->delete(array(
            'id' => $id
        ));
    }
}